package com.tjian.fafa.user.base.mainBody;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.bean.TabMsgItemEntity;
import com.tjian.fafa.user.base.chatServices.ChatServiceData;
import com.tjian.fafa.user.config.ConfigOfFafa;
import com.tjian.fafa.user.util.image.ImageUtil;

import java.util.List;

public class MainTabMsgAdapter extends BaseAdapter
{

    private List<TabMsgItemEntity> mVector;
    private LayoutInflater mInflater;
    private Context mContext0;

    //将传过来的对象，实例化为View类对象;
    public MainTabMsgAdapter(Context context, List<TabMsgItemEntity> vector)
    {
        this.mVector = vector;
        mInflater = LayoutInflater.from(context);//LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
        mContext0 = context;
    }

    //加载Adapter类会直接执行getView();
    public View getView(int position, View convertView, ViewGroup root)
    {
        ImageView avatarV;
        TextView unreadV;
        TextView nameOfSpeakerV;
        TextView textV;
        TextView timeV;

        String name = mVector.get(position).getName();
        int unread = 0;
        if (mVector.get(position).getTalkerId() > 0)
        {
            unread = ChatServiceData.getInstance().getUnreadMsgs(mVector.get(position).getTalkerId());
        }
        String sentence = mVector.get(position).getSentence();
        String time = mVector.get(position).getTime();
        String imagePath = mVector.get(position).getoAvatarImagePath();
        convertView = mInflater.inflate(R.layout.tabmsg_item, null);//对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.
        avatarV = (ImageView) convertView.findViewById(R.id.tabmsg_item_left_icon);
        unreadV = (TextView) convertView.findViewById(R.id.tabmsg_item_msg_indicator);
        nameOfSpeakerV = (TextView) convertView.findViewById(R.id.tabmsg_item_text_name);
        textV = (TextView) convertView.findViewById(R.id.tabmsg_item_text_latest_msg);
        timeV = (TextView) convertView.findViewById(R.id.tabmsg_item_text_time);

        String urlString = ConfigOfFafa.projectWebsiteUrl + imagePath;


        Bitmap map = null;
        if (imagePath != null && !imagePath.contentEquals("null"))
        {
            map = ImageUtil.getBitMapForStringURL(urlString);
        }

        avatarV.setImageBitmap(map);
        //	Bitmap bitmap = ImageUtil.getHttpBitmap("http://demohi.dayjian.com/imgs/avatar/2014/12/27/haoren1.jpg");//从网上取图片
        //	avatarV.setImageBitmap(bitmap);	//设置Bitmap
        if (unread == 0)
        {
            unreadV.setVisibility(View.INVISIBLE);
        }
        else
        {
            unreadV.setVisibility(View.VISIBLE);
            if (unread < 10)
            {
                unreadV.setText("" + unread);
            }
            else
            {
                unreadV.setText("9+");
            }
        }
        nameOfSpeakerV.setText(name);
        textV.setText(sentence);
        timeV.setText(time);

        return convertView;
    }

    public int getCount()
    {
        return mVector.size();
    }

    public Object getItem(int position)
    {
        return mVector.get(position);
    }

    public long getItemId(int position)
    {
        return position;
    }

    public Context getmContext0()
    {
        return mContext0;
    }

    public void setmContext0(Context mContext0)
    {
        this.mContext0 = mContext0;
    }
}